package com.zcf.service;

import com.zcf.mapper.PumpingLogMapper;
import com.zcf.mapper.RechargeMapper;
import com.zcf.pojo.PumpingLog;
import com.zcf.pojo.Recharge;
import com.zcf.vo.out.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-07-29
 */
@Service
public class RechargeServiceImpl {

    @Autowired
    private RechargeMapper rechargeMapper;

    public LayuiJson toDayRecharge(){
        LayuiJson lj=new LayuiJson();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        List<Recharge> recharges = rechargeMapper.selectByExample(beginOfDate, endOfDate);
        if (recharges.size()>0){
            int sum = recharges.stream().mapToInt(Recharge::getMoney).sum();
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("sumNum", sum);
            List<Map<String, Integer>> list = new ArrayList<Map<String,Integer>>();
            list.add(map);
            if(map != null){
                lj.setCode(0);
                lj.setCount(null);
                lj.setData(list);
                lj.setMsg("成功");
                return lj;
            }
        }
        lj.setCode(1);
        lj.setCount(null);
        lj.setData(null);
        lj.setMsg("暂无数据");
        return lj;
    }
    /**
    * @description :给与个人
    * @date : 2019/12/3 16:36
    * @author : jj
    */
    public int toDayRechargely(){
        LayuiJson lj=new LayuiJson();
        int sum=0;
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        List<Recharge> recharges = rechargeMapper.selectByExample(beginOfDate, endOfDate);
        if (recharges.size()>0){
            sum = recharges.stream().mapToInt(Recharge::getMoney).sum();
            return sum;

        }
        return sum;
    }

    /**
     * @description :今日个人充值记录
     * @date : 2019/12/3 16:36
     * @author : jj
     */
    public int userSumRecharges(Long userid){
        int sum=0;
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar1.getTime();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                23, 59, 59);
        Date endOfDate = calendar1.getTime();
        List<Recharge> recharges = rechargeMapper.userSumRecharges(userid,beginOfDate, endOfDate);
        if (recharges.size()>0){
            sum = recharges.stream().mapToInt(Recharge::getMoney).sum();
            return sum;

        }
        return sum;
    }
}
